<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 公告列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-lx-add"
                    class="handle-del mr10"
                    @click="handleAdd"
                >添加</el-button>
                <el-input v-model="query.title" placeholder="标题" class="handle-input mr10"></el-input>
                <el-input v-model="query.description" placeholder="简介" class="handle-input mr10"></el-input>
                <!-- <el-input v-model="query.createTime" placeholder="发布时间" class="handle-input mr10"></el-input> -->
                <el-select v-model="query.content" slot="prepend" clearable placeholder="状态" class="handle-input mr10">
                    <el-option label="已发布" value="0"></el-option>
                    <el-option label="未发布" value="1"></el-option>
                </el-select>
                <el-button type="primary" style="margin-right: 5px;" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <download-excel
                    class="export-excel-wrapper"
                    :data="multipleSelection"
                    :fields="json_fields"
                    :header="title"
                    name="公告数据.xls"
                    >
                    <el-button type="primary" icon="el-icon-lx-down" @click="handleExport">导出</el-button>
                </download-excel>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column prop="description" label="简介"></el-table-column>
                <el-table-column prop="createtime" label="创建时间"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">{{scope.row.status==0 ? '已发布':(scope.row.status==1 ? '未发布':'无')}}</template>
                </el-table-column>
                <el-table-column label="内容" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-info"
                            @click="handleShow(scope.$index, scope.row)"
                        >查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标题">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="简介">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <quill-editor ref="myTextEditor" v-model="form.content" :options="editorOption"></quill-editor>
                </el-form-item>
                <el-form-item label="状态">
                    <!-- <el-input v-model="form.status"></el-input> -->
                    <el-select v-model="form.status" clearable placeholder="请选择" style="width: 100%;">
                    <el-option label="已发布" value="0"></el-option>
                    <el-option label="未发布" value="1"></el-option>
                </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="公告内容" :visible.sync="contentVisible" width="50%">
            <el-card style="margin-bottom: 2%;">
                <p style="margin-bottom: 2%;">标题：</p>
                <div v-html="this.curForm.title" class="ql-editor"></div>
            </el-card>
            <el-card style="margin-bottom: 2%;">
                <p style="margin-bottom: 2%;">简介：</p>
                <div v-html="this.curForm.description" class="ql-editor"></div>
            </el-card>
            <el-card>
                <p style="margin-bottom: 2%;">内容：</p>
                <div v-html="this.curForm.content" class="ql-editor"></div>
            </el-card>
        </el-dialog>

        <!-- 添加弹出框 -->
        <el-dialog title="添加公告" :visible.sync="addVisible" width="50%">
            <el-form ref="addform" :model="addform" label-width="70px">
                <el-form-item label="标题">
                    <el-input v-model="addform.title"></el-input>
                </el-form-item>
                <el-form-item label="简介">
                    <el-input v-model="addform.description"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <!-- <el-input v-model="addform.content"></el-input> -->
                    <quill-editor ref="myTextEditor" v-model="addform.content" :options="editorOption"></quill-editor>
                    <!-- <el-button class="editor-btn" type="primary" @click="submit">提交</el-button> -->
                </el-form-item>
                <el-form-item label="状态">
                    <!-- <el-input v-model="form.status"></el-input> -->
                    <el-select v-model="addform.status" clearable placeholder="请选择" style="width: 100%;">
                    <el-option label="已发布" value="0"></el-option>
                    <el-option label="未发布" value="1"></el-option>
                </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getNoticeList, deleteNotice, addNotice, updateNotice } from '../../api/index';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import { quillEditor } from 'vue-quill-editor';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                current: 1,
                pageSize: 10,
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            contentVisible:false,
            curForm:'',
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            editorOption: {
                placeholder: '请输入公告内容'
            },
            title:'公告数据',
            json_fields: {
                'id': 'id',
                '标题': 'title',
                '简介': 'description',
                '内容': 'content',
                '状态': 'status',
            },
            addform: {
                'title': '',
                'description':'',
                'content':'',
            }
        };
    },
    components: {
        quillEditor
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            getNoticeList(this.query).then(response => {
                if(response.code != 0){
                    this.$message.error(response.message)
                }else{
                    let res = response.data
                    this.tableData = res.records;
                    this.pageTotal = res.total;
                }
                
            });
        },
        handleShow(index,row){
            console.log(row)
            this.curForm=row
            this.contentVisible=true
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        //导出表格
        handleExport() {
            console.log('导出表格')
            console.log(this.multipleSelection)
        },
        //添加公告
        saveAdd() {
            addNotice(this.addform).then(res => {
                console.log(res)
                if(res.code!=0){
                    this.$message.error(res.message)
                }else{
                    this.$message.success('添加成功!')
                    this.addVisible=false
                    this.getData() 
                }
                
            }).catch(err => {
                this.$message.error('添加失败，请检查信息是否正确!')
            })
        },
        handleAdd() {
            this.addVisible=true
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteNotice(row).then( res => {
                        if(res.code!=0){
                            this.$message.error(res.message)
                        }else{
                            this.$message.success('删除成功');
                            this.tableData.splice(index, 1);
                            this.pageTotal-=1
                        }
                    }).then(()=>{
                        
                    }).catch(err => {
                        this.$message.error('删除失败！')
                    })
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.form.status=this.form.status+''
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            updateNotice(this.form).then(res => {
                if(res.code!=0){
                    this.$message.error(res.message)
                    this.getData()
                }else{
                    this.$message.success(`修改第 ${this.idx + 1} 行成功!`);
                    this.$set(this.tableData, this.idx, this.form);
                }
                
            }).catch(err => {
                this.$message.error('修改第 ${this.idx + 1} 行失败!')
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    display: flex;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
